package com.zlx.sharelive.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_api.res_data.VideoBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_fg.BaseFg;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.Constants;
import com.zlx.sharelive.R;
import com.zlx.sharelive.activity.HomeSearchListAc;
import com.zlx.sharelive.activity.VideoDetailsAc;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.StringUt;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 个人主页——长视频
 */
public class HomeLongVideoFg extends BaseFg {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private String state = "1";
    private String type1 = "1";
    private int page = 1;
    private List<VideoBean.InfoDTO.RecordsDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<VideoBean.InfoDTO.RecordsDTO> mAdapter;


    public static HomeLongVideoFg getInstance(String id) {
        HomeLongVideoFg reviewFg = new HomeLongVideoFg();
        Bundle bundle = new Bundle();
        bundle.putSerializable("id", id);
        reviewFg.setArguments(bundle);
        return reviewFg;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fg_my_long_video;
    }

    @Override
    protected void initViews() {
        super.initViews();

        getVideoList(page);
        initRecyclerView();
    }

    private void getVideoList(int page) {
        ApiUtil.getHomeApi().getVideoList(SPUtil.getToken(getContext()),
                String.valueOf(page),
                type1,
                state,
                "",
                "",
                "1",
                (String) SPUtil.get(getContext(), Constants.SP_ID, ""),
                "",
                "",
                "1").observe(getViewLifecycleOwner(), new BaseObserver<>(new BaseObserverCallBack<ApiResponse<VideoBean>>() {
            @Override
            public void onSuccess(ApiResponse<VideoBean> data) {
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                if (null != data.getData().getInfo().get(0).getRecords()) {
                                    mList.addAll(data.getData().getInfo().get(0).getRecords());
                                }
                            }
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }


    /**
     * 初始化列表
     */
    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(getContext()));

        mAdapter = new CommonRecyclerViewAdapter<VideoBean.InfoDTO.RecordsDTO>(getContext(), mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, VideoBean.InfoDTO.RecordsDTO entity, int position) {

                GlideImgManager.glideLoader(getContext(), entity.getCover(), holder.getView(R.id.iv_recommend), 1);

                GlideImgManager.glideLoader(getContext(), entity.getAvatar(), holder.getView(R.id.iv_recommend_head), 0);

                holder.setText(R.id.tv_recommend_title, entity.getTitle());

                holder.setText(R.id.tv_recommend_nickname, entity.getNickname());

                holder.setText(R.id.tv_time, entity.getPeriod());

                if (entity.getIsLike().equals("1")) {
                    ((ImageView) holder.getView(R.id.iv_fabulous)).setImageResource(R.mipmap.fabulous_img_true);
                } else {
                    ((ImageView) holder.getView(R.id.iv_fabulous)).setImageResource(R.mipmap.fabulous_img_false);
                }

                if (entity.getIsVip().equals("1")) {
                    ((ImageView) holder.getView(R.id.iv_vip)).setVisibility(View.VISIBLE);
                } else {
                    ((ImageView) holder.getView(R.id.iv_vip)).setVisibility(View.GONE);
                }
                if (!StringUt.isEmpty(entity.getAuthentication())) {
                    if (entity.getAuthentication().equals("1")) {
                        ((ImageView) holder.getView(R.id.iv_goddess)).setVisibility(View.VISIBLE);
                    } else {
                        ((ImageView) holder.getView(R.id.iv_goddess)).setVisibility(View.GONE);
                    }
                }
                holder.setText(R.id.tv_pageViews, entity.getPageViews());
                holder.setText(R.id.tv_fabulous, entity.getLikeCount());
                holder.setText(R.id.tv_comment_count, entity.getCommentCount());
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_long_video;
            }
        };

        rv_data.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                startActivity(new Intent(getContext(), VideoDetailsAc.class)
                        .putExtra("id", mList.get(position).getId())
                        .putExtra("title", mList.get(position).getTitle()));
            }
        });
    }
}
